//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.package org.prx.view;

package org.prx.prp.view;

import org.prx.prp.R;
import org.prx.prp.utility.DatabaseAdapter;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleCursorTreeAdapter;

public class CategoriesListActivity extends ExpandableListActivity {

	private ExpandableListAdapter mAdapter;
	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			mContext = this;
			getExpandableListView().setHorizontalFadingEdgeEnabled(false);
			getExpandableListView().setBackgroundColor(
					Color.parseColor("#FFFFFF"));
			getExpandableListView().setCacheColorHint(
					Color.parseColor("#FFFFFF"));

			String query = "SELECT (NAME||' ('||COUNT(*)||')') AS CATEGORY_DISP_NAME,NAME AS CATEGORY_NAME,PROGRAMS.TITLE AS TITLE,PROGRAMS.ID AS PROGRAM_ID,CATEGORIES.ID as _id FROM CATEGORIES  JOIN CATEGORIZATIONS ON CATEGORIES.ID = CATEGORIZATIONS.CATEGORY_ID JOIN PROGRAMS ON PROGRAMS.ID=CATEGORIZATIONS.PROGRAM_ID AND PROGRAMS.PODCAST IS NOT NULL AND PROGRAMS.PODCAST!='' AND PROGRAMS.PODCAST !='NULL' GROUP BY(CATEGORY_ID) ORDER BY UPPER(TRIM(NAME))";
			Cursor groupCursor = DatabaseAdapter.executeQuery(query, null);
			startManagingCursor(groupCursor);

			mAdapter = new MyExpandableListAdapter(groupCursor,
					this,
					R.layout.my_expandable_list_item_1,
					R.layout.list_single_item_view,
					new String[] { "CATEGORY_DISP_NAME" }, // Name for group
															// layouts
					new int[] { android.R.id.text1 },
					new String[] { "PROGRAM_TITLE" }, // Number for child
														// layouts
					new int[] { R.id.text1 });
			setListAdapter(mAdapter);
			ExpandableListView outerListExpandableListView = getExpandableListView();
			setContentView(outerListExpandableListView);
			outerListExpandableListView.setOnChildClickListener(new OnChildClickListener() {
				@Override
				public boolean onChildClick(ExpandableListView arg0, View arg1,
						int group, int child, long arg4) {
					Cursor cursor = (Cursor) mAdapter.getChild(group, child);
					int programID = cursor.getInt(cursor.getColumnIndex("PROGRAM_ID"));
					Bundle bundle = new Bundle();
					bundle.putInt("PROGRAM_ID", programID);
					Intent intent = new Intent(mContext, ProgramInfoView.class);
					intent.putExtras(bundle);
					startActivity(intent);
					return false;
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

		public MyExpandableListAdapter(Cursor cursor, Context context,
				int groupLayout, int childLayout, String[] groupFrom,
				int[] groupTo, String[] childrenFrom, int[] childrenTo) {
			super(context, cursor, groupLayout, groupFrom, groupTo,
					childLayout, childrenFrom, childrenTo);
		}

		@Override
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			String categoryName = groupCursor.getString(groupCursor
					.getColumnIndex("CATEGORY_NAME"));
			Cursor c = DatabaseAdapter
					.executeQuery(
							"SELECT PROGRAMS.TITLE AS PROGRAM_TITLE,PROGRAMS.ID AS PROGRAM_ID,PROGRAMS.ID AS _id FROM CATEGORIES JOIN CATEGORIZATIONS ON CATEGORIES.ID = CATEGORIZATIONS.CATEGORY_ID JOIN PROGRAMS ON CATEGORIZATIONS.PROGRAM_ID=PROGRAMS.ID WHERE PROGRAMS.PODCAST IS NOT NULL AND PROGRAMS.PODCAST!='' AND PROGRAMS.PODCAST!='NULL' AND CATEGORIES.NAME='"
									+ categoryName + "'", null);
			startManagingCursor(c);
			return c;

		}
	}
}
